package com.zhiwei.popup

import android.content.Intent
import android.graphics.BitmapFactory
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.MotionEvent
import android.view.Window
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import java.io.File
import java.io.FileOutputStream

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main)

        val txv = findViewById<TextView>(R.id.txv);
        txv.setOnClickListener {
            val intent = Intent(baseContext, ImageActivity::class.java)
            startActivity(intent)
//            copyAssertFile2Storage("image")
        }

        val colorPicker = findViewById<ColorPickerView>(R.id.colorPicker)
        colorPicker.setOnColorChangedListener { r, g, b, color ->
            println("r $r  g $g  b $b")
            txv.setBackgroundColor(color);
        }

    }

    private fun copyAssertFile2Storage(dir: String) {
        val files: Array<String> = assets.list(dir)
        for(file in files){
            println(file)
            copy2CacheDir(file)
        }
    }

    fun copy2CacheDir(fileName: String){
        val cacheDir = externalCacheDir.path
        val inputStream = assets.open(fileName)
        val fos = FileOutputStream(File("$cacheDir/$fileName"))
        inputStream.copyTo(fos, 1024)
        inputStream.close()
        fos.close()
    }

    fun AppCompatActivity.showToast(msg: CharSequence, duration: Int = Toast.LENGTH_SHORT) =
            Toast.makeText(this, msg, duration).show()
}
